var platform = {

    iPhone: function () {
        if ((navigator.userAgent).indexOf('iPhone') > -1) {
            return true;
        } else {
            return false;
        }
    },
    iPad: function () {
        if ((navigator.userAgent).indexOf('iPad') > -1) {
            return true;
        } else {
            return false;
        }
    },
    Android: function () {
        if ((navigator.userAgent).indexOf('Android') > -1) {
            return true;
        } else {
            return false;
        }
    },
    win: function () {
        if ((navigator.userAgent).indexOf('Windows') > -1) {
            return true;
        } else {
            return false;
        }
    },
    mac: function () {
        if ((navigator.userAgent).indexOf('mac') > -1) {
            return true;
        } else {
            return false;
        }
    },
    pc: function () {
        if (this.win() || this.mac()) {
            return true;
        } else {
            return false;
        }
    },
    phone: function () {
        if (!this.pc()) {
            return true;
        } else {
            return false;
        }
    }
}
var imgs = {};
var state = {
    'Loading': Loading,
    'Opening': Opening,
    'Begin': Begin,
    'P1': P1,
    'P2': P2,
    'P3': P3,
    'P4': P4,
    'P5': P5,
    'P6': P6,
    'P7': P7,
    'Final': Final

};
var statekey = [
    'Loading',
    'Opening',
    'Begin',
    'P1',
    'P2',
    'P3',
    'P4',
    'P5',
    'P6',
    'P7',
    'Final'
];
var index = 0;
var flag = statekey.map(function (item, i) {
    if (i === 0) {
        return 2;
    } else {
        return 0;
    }
});
console.log(flag)

var iflag='1';  // iphone第一次打开是竖屏
var game;


window.onload = function () {
    game = new Phaser.Game(
        1624,
        750,
        Phaser.AUTO,
        'game',
        true
    );
    game.goState = function (arg) {
        if (arg === 'next') {
            index = index + 1;
        }
        if (arg === 'back') {
            index = index - 1;
        }
        if (typeof arg === 'number') {
            index = arg;
        }
        game.state.start(statekey[index]);
        flag[index] = 2;
        var str = flag.join('_');
        console.log(str)
        var susdata = doLMSSetValue("cmi.suspend_data", str);
        if (str.indexOf('0') === -1) {
            console.log('completed')
            doLMSSetValue("cmi.core.lesson_status", "completed");
            doLMSCommit();
        }

    }
    Object.keys(state).map(function (key) {
        game.state.add(key, state[key]);
    });
    game.state.start(statekey[index]);

    match = window.matchMedia("(orientation:portrait)");

    resize();

    window.addEventListener('resize', function () {
        var res;
        if (res){clearTimeout(res)}
        res = setTimeout(resize,20);
    }, false);

    window.addEventListener('unload', function (e) {
        var status = this.doLMSGetValue("cmi.core.lesson_status");
        doLMSCommit();
        unloadPage(status);
    });



    setTimeout(function () {
        var str = flag.join('_');
        loadPage(str, '2');
        var status = doLMSGetValue("cmi.core.lesson_status");
        var susdata = doLMSGetValue("cmi.suspend_data");
    }, 100);




}

function resize() {

        var mask = document.getElementById('screen');
        var div = document.getElementById('game');

        if (platform.Android()) {

            if (window.innerWidth > window.innerHeight) {
                mask.style.display = 'none';
                div.style.width = window.screen.width + 'px';
                div.style.height = (window.screen.width * 750 / 1624).toFixed(0) + 'px';
            } else {
                mask.style.display = 'block';
                mask.style.width = '100%';
                mask.style.height = '100%';
            }
        }

        if (platform.pc()) {
            mask.style.display = 'none';
            div.style.width = '1300px';
            div.style.height = '600px';
            div.style.left = (window.innerWidth - 1300) / 2 + 'px';
        }

        if (platform.iPhone()) {

            if (iflag === '1') {
                mask.style.display = 'block';
                mask.style.width = window.screen.width + 'px';
                mask.style.height = '100%';
                iflag = '--';
            } else {
                mask.style.display = 'none';
                div.style.width = window.screen.height + 'px';
                div.style.height = (window.screen.height * 750 / 1624).toFixed(0) + 'px';
                iflag = '1';
            }

        }

}






